'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskreviewCtrl
 * @description
 * # TaskreviewCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('TaskreviewCtrl', function ($scope, UserInfo, $routeParams, $filter, $rootScope, logList, TaskReviewService, DataService, Util, openApi, submitAudit) {
        // 详情接口，审批记录接口，提交审核接口
        var id = $routeParams.id;
        $scope.id = id;
        var logUrl, detailDataUrl, submiturl;
        var TVtype;
        TVtype = Util.getTVType();
        switch (TVtype) {
            case Util.CHECK://待审批
                detailDataUrl = openApi.allDetailApi;
                logUrl = openApi.myProcessLog;
                break;
            case Util.READ://无需审批
                detailDataUrl = openApi.getTaskInfo;
                // logUrl = openApi.myProcessLog;
                break;
            case Util.AUDIT://审批
                detailDataUrl = openApi.getTaskInfo;
                logUrl = openApi.myProcessLogA;
                submiturl = openApi.completeAudit
                break;
            case Util.SYNCAUDIT://并行审批
                detailDataUrl = openApi.getTaskInfo;
                logUrl = openApi.myProcessLogA;
                submiturl = openApi.completeSyncAduit
                break;
            case Util.CAUDIT://自定义流程审批
                detailDataUrl = openApi.getTaskInfo;
                logUrl = openApi.myProcessLogA;
                submiturl = openApi.completeCaduitObject
                break;
            default:

        }
        $scope.auditText = "";
        $scope.check = function (item) {
            // $scope.auditText = item.name;//不显示默认值
            $scope.operationId = item.id;
            $scope.showDailog = true;
            $rootScope.showMasker = true;
        };
        $scope.confirm = function () {
            $scope.showDailog = false;
            $rootScope.showMasker = false;
            completeAudit();
        };
        $scope.cancel = function () {
            console.log("cancel")
            $scope.showDailog = false;
            $rootScope.showMasker = false;
        };
        $scope.hrefLink = function (href) {
            // ios & android are opened in the different way
            if (window.device && window.device.platform == "iOS") {
                window.open(href, '_blank', 'location=yes');
            } else {
                window.open(href, '_system', 'location=no');
            }
        }
        var completeAudit = function () {
            var param = {};
            param.taskId = $scope.TaskReview.result.taskId;
            param.operationId = $scope.operationId;
            param.attachmentDesc = $scope.auditText;
            param.attachmentName = $scope.attachmentName || "";
            param.url = submiturl;
            new submitAudit(param);

        };
        // 获取回显的详细信息
        var getDetailData = function (callback) {
            var options = {};
            options.param = {};
            options.param.processInstanceId = id;
            options.param.taskId = id;
            options.url = detailDataUrl;
            options.callback = callback;
            var TaskReview = new TaskReviewService(options);
            $scope.TaskReview = TaskReview;
        };
        // 获取日志信息
        var getLogListData = function () {
            var param = {}
            var options = {};
            options.param = {};

            options.url = logUrl;
            options.param.processId = id;
            options.param.taskId = id;
            $scope.logData = new logList(options);
        };
        // 该流程无需审批，设置查看回执
        var setDetailSeen = function (processInstanceId) {
            var param = {};
            param.noloader = true;
            param.processInstanceId = processInstanceId;
            DataService.getData(openApi.publicDoc, param).then(function (data) {
            })

        };
        var settaskRemind = function (taskData) {
            UserInfo.getUseinfo().then(function (Userdata) {
                var checked = false;
                checked = taskData.processStatus == '审批中' && Userdata.userId == taskData.userId;
                return $scope.showTaskRemind = checked
            });
        }
        // 如果是审批中并且当前登录用户是当前发起人，可以显示提醒一下
        var init = function () {
            switch (TVtype) {
                case Util.CHECK:
                    getDetailData(function (data, form_key) {
                        // 设置formKey用来进入关联模块
                        $scope.form_key = form_key;
                        // 设置提醒审批
                        settaskRemind(data)
                    });
                    getLogListData();
                    break;
                case Util.READ://通知发文无需显示审批记录
                    getDetailData(function (data, form_key) {
                        // 设置formKey用来进入关联模块
                        $scope.form_key = form_key;
                        setDetailSeen(data.processInstanceId);
                    });
                    break;
                case Util.AUDIT:
                case Util.SYNCAUDIT:
                case Util.CAUDIT:
                    getDetailData(function (data, form_key) {
                        // 根据返回数据显示按钮
                        Util.showAudit({
                            scope: $scope
                        });
                        Util.showAuditDailog({
                            scope: $scope
                        });
                        // 显示添加附件
                        Util.showAddFile({
                            scope: $scope
                        });
                    });
                    getLogListData();
                    break;
            }
        };
        // 初始化
        init();
    });
